package com.hospital.wx.service.Impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateRange;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.hospital.wx.dao.DoctorWorkPlanDao;
import com.hospital.wx.service.DoctorWorkPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class DoctorWorkPlanServiceImpl implements DoctorWorkPlanService {

    @Autowired
    private DoctorWorkPlanDao doctorWorkPlanDao;

    /*
     * 根据诊室ID+日期查询医生出诊计划信息
     */
    @Override
    public ArrayList<String> selectDoctorScheduleBySubIdAndDate(Map param) {
        /*
         * 获取医生排班列表
         * list =>
         * ["2025-02-21","2025-02-22","2025-02-23"]
         * */
        ArrayList<String> list = doctorWorkPlanDao.selectDoctorScheduleBySubIdAndDate(param);
        //解析出endDate和startDate
        DateTime startDate = DateUtil.parse(MapUtil.getStr(param, "startDate"));
        DateTime endDate = DateUtil.parse(MapUtil.getStr(param, "endDate"));

        /*
         *
         * 基于startDate-endDate生成日期范围
         * 例如：  2024-05-07 - 2024-05-13
         */
        DateRange range = DateUtil.range(startDate, endDate, DateField.DAY_OF_MONTH);

        //创建结果集
        ArrayList result = new ArrayList();

        /*
        * 遍历range生成最终结果集
        * */
        while (range.hasNext()) {
            String date = range.next().toDateStr();
            if (list.contains(date)) {
                result.add(new HashMap<>() {{
                    put("date", date);
                    put("status", "出诊");
                }});
            } else {
                result.add(new HashMap<>() {{
                    put("date", date);
                    put("status", "无号");
                }});
            }
        }
        return result;
    }

    @Override
    public ArrayList<HashMap> selectDoctorShiftDetailsBySubIdAndDate(Map param) {
        return doctorWorkPlanDao.selectDoctorShiftDetailsBySubIdAndDate(param);
    }

}
